package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractListModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class HomeUI extends JFrame {

	private static final long serialVersionUID = 0xA1L;

	private JList<String> list_action;
	private JMenu mn_action;
	private JMenu mn_help;
	private JMenuBar mnbar;
	private JMenuItem mni_about;
	private JMenuItem mni_checknewmail;
	private JMenuItem mni_exit;
	private JMenuItem mni_inbox;
	private JMenuItem mni_newmail;
	private JPanel pn_content;
	private JPanel pn_menu;
	private JScrollPane scroll_list_action;
	private JSplitPane sp_home;
	
	private static HomeUI instance;
	
	public static final HomeUI getInstance(){
		if(instance == null)
			return instance = new HomeUI();
		return instance;
	}

	private HomeUI() {
		initComponents();
	}

	private void newComps() {
		sp_home = new JSplitPane();
		pn_menu = new JPanel();
		scroll_list_action = new JScrollPane();
		list_action = new JList<>();
		pn_content = new JPanel();
		mnbar = new JMenuBar();
		mn_action = new JMenu();
		mni_newmail = new JMenuItem();
		mni_checknewmail = new JMenuItem();
		mni_inbox = new JMenuItem();
		mni_exit = new JMenuItem();
		mn_help = new JMenu();
		mni_about = new JMenuItem();
	}

	@SuppressWarnings({ "unchecked", "serial", "rawtypes" })
	private void setCompsProperties() {
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setIconImage(new ImageIcon("src/icons/soft_icon_32x32.png").getImage());
		setTitle("CA_Team Mail Client");
		

		sp_home.setPreferredSize(new Dimension(1300, 670));

		pn_menu.setMinimumSize(new Dimension(200, 670));
		pn_menu.setPreferredSize(new Dimension(200, 670));

		scroll_list_action.setBorder(null);

		list_action.setBackground(Color.white);
		list_action.setModel(new AbstractListModel() {
			String[] strings = { "New Mail", "Inbox", "Sent" };

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		scroll_list_action.setViewportView(list_action);

	}

	private void pn_menuLayout() {
		GroupLayout pn_menuLayout = new GroupLayout(pn_menu);
		pn_menu.setLayout(pn_menuLayout);
		pn_menuLayout.setHorizontalGroup(pn_menuLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addComponent(scroll_list_action,
				GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE));
		pn_menuLayout.setVerticalGroup(pn_menuLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addComponent(scroll_list_action,
				GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE));

		sp_home.setLeftComponent(pn_menu);

	}

	private void pn_contentLayout() {
		pn_content.setBackground(Color.white);
		pn_content.setPreferredSize(new Dimension(1100, 670));

		GroupLayout pn_contentLayout = new GroupLayout(pn_content);
		pn_content.setLayout(pn_contentLayout);
		pn_contentLayout.setHorizontalGroup(pn_contentLayout
				.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0,
						1094, Short.MAX_VALUE));
		pn_contentLayout.setVerticalGroup(pn_contentLayout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGap(0, 670, Short.MAX_VALUE));

		sp_home.setRightComponent(pn_content);
	}

	private void setMenu() {
		mn_action.setText("Action");

		mni_newmail.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
				InputEvent.ALT_MASK));
		mni_newmail.setText("Compose Mail");
		mn_action.add(mni_newmail);

		mni_checknewmail.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
				InputEvent.ALT_MASK));
		mni_checknewmail.setText("Check Mail");
		mn_action.add(mni_checknewmail);

		mni_inbox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
				InputEvent.ALT_MASK));
		mni_inbox.setText("Open Inbox");
		mn_action.add(mni_inbox);

		mni_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
				InputEvent.ALT_MASK));
		mni_exit.setText("Exit");
		mn_action.add(mni_exit);

		mnbar.add(mn_action);

		mn_help.setText("Help");
		mn_help.setToolTipText("");

		mni_about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0));
		mni_about.setText("About");

		mn_help.add(mni_about);

		mnbar.add(mn_help);

		setJMenuBar(mnbar);
	}
	
	private void setMainLayout()
	{
		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addComponent(sp_home,
				GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addComponent(sp_home,
				GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE));

		pack();
		this.setLocationRelativeTo(null);
	}

	private void initComponents() {
		newComps();
		setCompsProperties();
		pn_menuLayout();
		pn_contentLayout();
		setMenu();
		setMainLayout();
		
	}

	public static void main(String args[]) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			new HomeUI().setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
